<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class myoutube extends MY_Model {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
    public $mytable = "youtube";
    public $primaryKey = "YOUT_ID";
    public $resCode = "YOUTUBE";

    function __construct() {
        parent::__construct();
		$this->load->library('get_data_html');
    }

    function field() {
		$fieldData = array();
		$fieldData['YOUT_TYPE'] = '';
		$fieldData['YOUT_LINK'] = '';
		$fieldData['YOUT_TITLE'] = '';
		$fieldData['YOUT_PARENT'] = 0;
		$fieldData['MY_ID'] = 0;
		$fieldData['CAT_ID'] = 0;
		$fieldData['YOUT_STATUS'] = 1;
		$fieldData['YOUT_TIME_CREATE'] = time();
		return $fieldData;
    }
	
	function getUser($val = '', $param = null){
		$this->db->where('YOUT_TYPE','user');
		return $this->get($val,$param );
	
	}
	function getsUser($val = '', $param = null){
		$this->db->where('YOUT_TYPE','user');
		return $this->gets($val,$param );
	
	}
	
	function getPlayList($val = '', $param = null){
		$this->db->where('YOUT_TYPE','playlist');
		return $this->get($val,$param );
	
	}
	function getsPlayList($val = '', $param = null){
		$this->db->where('YOUT_TYPE','playlist');
		return $this->gets($val,$param );
	
	}
	
	function getsVideo($val = '', $param = null){
		$this->db->where('YOUT_TYPE','video');
		$this->db->order_by("YOUT_STATUS asc");
		return $this->gets($val,$param );
	
	}
	
	function getVideo($val = '', $param = null){
		$this->db->where('YOUT_TYPE','video');
		return $this->get($val,$param );
	
	}

	function getNumPlayList($userName){
		
	}
	
	function getPlaylistByUser(&$arrPlayList, $userName = '', $youtID = 0,$fullLink = ''){

		if($fullLink == ''){
			$start_from = 1;
			$import_results = 50;
			$fullLink = "http://gdata.youtube.com/feeds/api/users/".$userName."/playlists?start-index=".$start_from."&max-results=".$import_results;
		}
		
		
		$data = $this->get_data_html->get_content($fullLink);
		$x = @simplexml_load_string( $data );
		
		foreach($x->entry as $r){
			$arrPlayList[] = array(
					//'YOUT_LINK'=>(string)$r->id,
					'YOUT_TITLE'=> (string)$r->title,
					'YOUT_LINK'=> (string)$r->link[1]->attributes()->href[0],
					'YOUT_PARENT'=>$youtID ,
					'YOUT_TYPE'=>'playlist',
				);
			}

			for($i=0; $i<20; $i++){
				$r = (array)$x->link[$i];
				if(count($r)>0){
					if($r['@attributes']['rel'] == 'next'){
						return $this->getPlaylistByUser($arrPlayList,'',$youtID,$r['@attributes']['href']);
					}
				}
				
			}
		return $arrPlayList;
	}
	
	function insertPlayList($arrInsert = array()){
		$result = array();
		if(count($arrInsert)>0)
		foreach($arrInsert as $r){
			$check =$this->get($r["YOUT_LINK"],'YOUT_LINK');
			if(!$check){
				$this->insert($r);
				$result[] =$r;
			}
		}
		return $result;
	}
	
	function getVideoByPlayList(&$arrPlayList, $link = '', $youtID = 0, $starIndex =1){
		$num = 50;
		$playlistID = str_replace("http://www.youtube.com/playlist?list=","",$link);
		$url = 'http://gdata.youtube.com/feeds/api/playlists/'.$playlistID.'?v=2&alt=jsonc&start-index='.$starIndex.'&max-results='.$num;
		$data = $this->get_data_html->get_content($url);
		$data = json_decode($data);
		if(isset($data->data->items))
		foreach($data->data->items as $r){
			$video = $r->video;
			$arrPlayList[] = array(
					//'YOUT_LINK'=>(string)$r->id,
					'YOUT_TITLE'=> $video->title,
					'YOUT_LINK'=> $video->id,
					'YOUT_PARENT'=>$youtID ,
					'YOUT_TYPE'=>'video',
				);
			}
			$nextNum = $starIndex + $num;
			if(@$data->data->totalItems > $starIndex){
				return $this->getVideoByPlayList($arrPlayList, $link,$youtID,$nextNum);
			}
			
		return $arrPlayList;
	}
	
	function insertVideos($arrInsert = array()){
		$result = array();
		if(count($arrInsert)>0)
		foreach($arrInsert as $r){
			$check =$this->get($r["YOUT_LINK"],'YOUT_LINK');
			if(!$check){
				$this->insert($r);
				$result[] =$r;
			}
		}
		return $result;
	}

	function getVideoByUser(&$arrPlayList, $userName = '', $youtID = 0,$fullLink = ''){
		$num = 2;
		if($fullLink == ''){
			$start_from = 1;
			$fullLink = "http://gdata.youtube.com/feeds/api/users/".$userName."/uploads?start-index=".$start_from."&max-results=".$num."&alt=json&v=2";
		}
		
		$data = $this->get_data_html->get_content($fullLink);
		$data = json_decode( $data );
		$feed =   (array)$data->feed;
		$total = (array)$feed['openSearch$totalResults'];
		$total = (int)$total['$t'];
	  
		
		
		foreach($feed['entry'] as $r){
			 $r = (array)$r;
			 $title = (array)$r['title'];
			 $group = (array)$r['media$group'];
			 $youtubeID = (array)$group['yt$videoid'];
			 $videoTime = (array)$group['yt$duration'];
			 //time: $videoTime['seconds'];
			
			$arrPlayList[] = array(
					'YOUT_TITLE'=>  trim($title['$t']),
					'YOUT_LINK'=> trim($youtubeID['$t']),
					'YOUT_PARENT'=>$youtID ,
					'YOUT_TYPE'=>'video',
				);
			}
		
			for($i=0; $i<20; $i++){
				$r = (array)@$feed['link'][$i];
				if(count($r)>0){
					if($r['rel'] == 'next'){
						return $this->getVideoByUser($arrPlayList,'',$youtID,$r['href']);
					}
				}
			}
		return $arrPlayList;
	}
	
	function pareTimeYoutube($youtubeID){
		$this->load->library("get_data_html");
		$html = $this->get_data_html->get_content("http://gdata.youtube.com/feeds/api/videos/".$youtubeID);
		if($html =='Video not found')
			return -1;
		if($html =='Invalid id')
			return -2;
		if($html =='Private video')
			return -3;
		
		$time = $this->get_data_html->GetBetween($html,"duration='","'");
		return $time;
	}
}

?>